package com.wwdx.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwdx.model.WwdxBookChapterModel;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.dto.BookBusinessDTO;
import com.wwdx.service.WwdxBookServiceI;
import com.wwdx.utils.ConstantHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author liguoliang
 */
@Service
public class WwdxBookServiceImpl extends BaseServiceImpl<WwdxBookModel> implements WwdxBookServiceI {

    @Override
    public Long selectModelCount(WwdxBookModel model){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "select count(1) from WwdxBookModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxBookModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        String orderBy = "\torder by gmtCreated desc";
        //书籍ID查询
        Long bookid = model.getBookid();
        if (bookid != null){
            sb.append("and bookid=:bookid\n");
            params.put("bookid",bookid);
        }
        //书籍名称查询
        String bookname = model.getBookname();
        if (StringUtils.isNotBlank(bookname)){
            sb.append("and bookname like :bookname\n");
            params.put("bookname","%"+bookname+"%");
        }
        //是否上线查询
        Integer status = model.getStatus();
        if (status != null){
            sb.append("and status=:status\n");
            params.put("status",status);
        }
        //撰稿人或音频转述师下的书籍查询
        Long userid = model.getUserid();
        if (userid != null){
            sb.append("and (videoauthorid=:userid or copywriter=:userid)\n");
            params.put("userid",userid);
        }
        //书籍关键字查询
        String word = model.getWord();
        if (StringUtils.isNotBlank(word)){
            sb.append("and (bookname like :word or bookauthor like :word)\n");
            params.put("word","%"+word+"%");
        }
        //是否首页显示查询
        Integer ishost = model.getIshost();
        if (ishost != null){
            orderBy = "\tORDER BY sort desc";
            params.put("ishost",ishost);
            sb.append("and ishost=:ishost\t");
        }
        //书籍类型查询
        Integer type = model.getType();
        if (type != null){
            params.put("type",type);
            sb.append("and type=:type\t");
        }
        //主题下的书籍查询
        Long themeid = model.getThemeid();
        if (themeid != null){
            WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(themeid);
            String bookList = wwdxThemeModel.getBookList();
            Iterable<String> iterable = Splitter.on(",").omitEmptyStrings().trimResults().split(bookList);
            List<Long> bookids = Lists.newArrayListWithExpectedSize(16);
            iterable.forEach(id -> bookids.add(Long.valueOf(id)));
            params.put("ids",bookids);
            sb.append("and bookid IN (:ids)\n");
        }
        //随机获取书籍数查询
        Boolean random = model.getRandom();
        if (random != null && random){
            orderBy = "\torder by rand()";
        }
        sb.append(orderBy);
        return sb.toString();
    }

    @Override
    public List<WwdxBookModel> selectModelList(WwdxBookModel model, Integer page, Integer rows){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(6);
        String hql = "from WwdxBookModel\t"+getParams(model,params);
        return find(hql,params,page,rows);
    }

    @Override
    public WwdxBookModel getByBookId(Long bookid){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("bookid",bookid);
        return getByHql("from WwdxBookModel where bookid=:bookid", params);
    }

    @Override
    public List<BookBusinessDTO.MerchantBookListDTO> findBookList(){
        List<Integer> ids = Lists.newArrayList(9571,13032,85766,216539,13051,2378,99599,
                5327,7449,2445,216473,134564,171155,171156, 171157,
                171158,124667,214316,1504249504,13047,1504253430,
                1504601515,190174,2273,1504344543,1504419817,1504411479,
                1504408007,10901,208304,12988,1504424817,1504426925,
                110722,55927,1968,12932,217221,3150,13011,217536,
                87666,141925,2574,99591,77083,5665,9058,131462,1504349969,
                1504441850,1504451409,1504447134,217943,2602,95256,12743,
                116166,81674,200675,1504510401,1504506194,2287,1504496955,1504492719,
                6119,9401,6107,1504513333,8619,1504502552,1504506016,216840,1504493173,
                1504508705,1504511779,1504506569,213994,1504515694,157164,217822,157164,
                480,159957,216864,150,216542,10987,1504690297,1504693113,83366,89654,
                11149,13044,2341,3321,109609);
        List<BookBusinessDTO.MerchantBookListDTO> bookListDTOS = (List<BookBusinessDTO.MerchantBookListDTO>) valueOperations.get("merchant:booklist");
        if (bookListDTOS != null){
            return bookListDTOS;
        }
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("ids",ids);
        String sql = "select bookid,bookname,bookimg from wwdx_book where bookid in (:ids)";
        List<Map> list = super.findBySql(sql, params);
        bookListDTOS = Lists.newArrayListWithExpectedSize(list.size());
        BookBusinessDTO.MerchantBookListDTO dto = new BookBusinessDTO.MerchantBookListDTO();
        for (Map map : list) {
            String bookimg = map.get("bookimg").toString();
            String bookname = map.get("bookname").toString();
            Long bookid = Long.valueOf(map.get("bookid").toString());
            WwdxBookChapterModel wwdxBookChapterModel = wwdxBookChapterService.getByBookId(bookid);
            if (wwdxBookChapterModel == null){
                continue;
            }
            dto = (BookBusinessDTO.MerchantBookListDTO) dto.clone();
            dto.setBookimg(picUrl + ConstantHolder.Constant.COVER + bookimg);dto.setBookname(bookname);
            String chaptervideo = wwdxBookChapterModel.getChaptervideo();
            dto.setAudioAddress(picUrl + ConstantHolder.Constant.COVER + chaptervideo);
            bookListDTOS.add(dto);
        }
        valueOperations.set("merchant:booklist",bookListDTOS);
        redisTemplate.expire("merchant:booklist", 7 * 24 * 60 * 60, TimeUnit.SECONDS);
        return bookListDTOS;
    }
}